package tools

import (
	"log"
	"prometheus_import_consul/global"

	"github.com/hashicorp/consul/api"
)

func Add_host(ip, env, HostType, businesscode string) {
	// 构造服务注册的配置
	registration := &api.AgentServiceRegistration{
		ID:      global.Config.Consul.ServiceName[0] + ip, // 唯一服务ID
		Name:    global.Config.Consul.ServiceName[0],      // 服务名称
		Address: ip,
		Tags:    []string{global.Config.Consul.ServiceName[0]},
		Meta: map[string]string{
			"ip":            ip,
			"env":           env,
			"type":          HostType,
			"business_code": businesscode,
		},
	}

	// 使用Consul客户端注册服务
	if err := global.ConsulClient.Agent().ServiceRegister(registration); err != nil {
		log.Fatalf("Error registering host with Consul:%v\n", err)
		return
	}
	log.Printf("Host %s registered successfully\n", ip)
}
